import 'dart:typed_data';

class BleUtils {
  static String makeChecksum(String data) {
    if (data == null || data.isEmpty) {
      return "00";
    }
    int iTotal = 0;
    int iLen = data.length;
    int iNum = 0;
    while (iNum < iLen) {
      String s = data.substring(iNum, iNum + 2);
      int t = int.tryParse(s, radix: 16);
      iTotal += int.tryParse(s, radix: 16);

      iNum = iNum + 2;
    }

    /**
     * 用256求余最大是255，即16进制的FF
     */
    int iMod = iTotal % 256;
    String sHex = iMod.toRadixString(16);
    iLen = sHex.length;
    //如果不够校验位的长度，补0,这里用的是两位校验
    if (iLen < 2) {
      sHex = "0" + sHex;
    }
    return sHex;
  }

  static String bytesToHexString(List<int> bytes) {
    StringBuffer sb = StringBuffer();

    for (int byte in bytes) {
      String hex = byte.toRadixString(16);
      if (hex.length == 1) {
        sb.write('0');
      }
      sb.write(hex);
    }

    return sb.toString().toUpperCase();
  }

  static String encode2(String var0) {
    int var1 = 90;
    List<int> var2 = hexStringToByte(var0);

    for (int var3 = 0; var3 < var2.length; ++var3) {
      var2[var3] = ((var2[var3] ^ var1) + 1);
    }

    return bytesToHexString(var2);
  }

  static List<int> encode1(String var0) {
    int var1 = 90;
    List<int> var2 = hexStringToByte(var0);

    for (int var3 = 0; var3 < var2.length; ++var3) {
      var2[var3] = ((var2[var3] ^ var1) + 1);
    }

    return encode(var2);
  }

  static List<int> encode(List<int> var0) {
    int var1 = 90;

    for (int var2 = 0; var2 < var0.length; ++var2) {
      var0[var2] = ((var0[var2] ^ var1) + 16);
    }

    return var0;
  }

  static String sortToString(String var0) {
    StringBuffer var1 = StringBuffer();

    for (int var2 = 0; var2 < var0.length / 2; ++var2) {
      if (var0.length < 2) {
        var1.write(var0);
      } else {
        var1.write(var0.substring(
            var0.length - (2 + 2 * var2), var0.length - 2 * var2));
      }
    }

    return var1.toString();
  }

  static String patchHexString(String var0, int var1) {
    String var2 = "";

    for (int var3 = 0; var3 < var1 - var0.length; ++var3) {
      var2 = "0" + var2;
    }

    var0 = (var2 + var0).substring(0, var1).toUpperCase();
    var0 = sortToString(var0);
    return var0;
  }

  static List<int> hexStringToByte(String var0) {
    List<int> byteArray = List.generate(var0.length ~/ 2, (index) {
      String byteString = var0.substring(index * 2, index * 2 + 2);
      return int.parse(byteString, radix: 16);
    });
    return byteArray;
  }

  static int byteArraytoInt(Int64List bRefArr) {
    int iOutcome = 0;

    for (int i = 0; i < bRefArr.length; i++) {
      int bLoop = bRefArr[i];
      iOutcome += ((bLoop & 0xFF) << 8 * i);
    }
    return iOutcome;
  }

  static Int64List InttoByteArray(int iSource, int iArrayLen) {
    Int64List bLocalArr = Int64List(iArrayLen);
    for (int i = 0; (i < 4) && (i < iArrayLen); i++) {
      bLocalArr[i] = (iSource >> 8 * i & 0xFF);
    }
    return bLocalArr;
  }

  static Int64List CRC16(Int64List source) {
    Int64List reg = Int64List(2);
    Int64List ploy = Int64List(2);

    int temp = 65535;
    ploy = InttoByteArray(temp, 2);

    temp = 65535;
    reg = InttoByteArray(temp, 2);
    for (int i = 0; i < source.length; i++) {
      var data = source[i];
      ploy[0] = reg[1];
      ploy[1] = reg[0];
      ploy[0] = (ploy[0] ^ data);
      ploy[1] = (ploy[1] ^ 0x0);
      reg = InttoByteArray(
          byteArraytoInt(ploy) ^ (byteArraytoInt(ploy) & 0xFF) >> 4, 2);
      reg = InttoByteArray(byteArraytoInt(reg) ^ byteArraytoInt(reg) << 12, 2);
      reg = InttoByteArray(
          byteArraytoInt(reg) ^ (byteArraytoInt(reg) & 0xFF) << 5, 2);
    }
    return reg;
  }

  ///CRC16校验  list是需要增加校验位的数据
  static String getCRC(String data) {
    List<int> list = hexStringToByte(data);
    Int64List value = Int64List.fromList(list);
    return bytesToHexString(CRC16(value)).toUpperCase();
  }
}
